//
//  TakeViewController.m
//  SweetComics
//
//  Created by LeeZhe on 15/1/28.
//  Copyright (c) 2015年 LeeZhe. All rights reserved.
//

#import "TakeViewController.h"
#import "LoginViewController.h"
#import "SearchViewController.h"


@interface TakeViewController ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UISegmentedControl *seg;

@end

@implementation TakeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 订阅的标题
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"订阅", @"浏览记录"]];
    self.navigationItem.titleView = self.seg;
    // 默认seg的选项
    self.seg.selectedSegmentIndex = 0;
    [self.seg addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0 green:0.5 blue:1 alpha:0.7];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(barButtonAction1:)]autorelease];
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(barButtonAction2:)]autorelease];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    [self.view addSubview:self.scrollView];
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    [_scrollView release];
    
    self.view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _view1.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:self.view1];
    [_view1 release];
    
    self.view2 = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _view2.backgroundColor = [UIColor blueColor];
    [_scrollView addSubview:self.view2];
    [_view2 release];
    
}
// 搜索按钮
- (void)barButtonAction1:(UIBarButtonItem *)button
{
    self.hidesBottomBarWhenPushed = YES;
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    [searchVC release];
}
// 登陆按钮
- (void)barButtonAction2:(UIBarButtonItem *)button
{
    self.hidesBottomBarWhenPushed = YES;

    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [self.navigationController pushViewController:loginVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;

    [loginVC release];
}
// seg绑定的方法
- (void)segmentAction:(UISegmentedControl *)seg
{
    NSInteger Index = seg.selectedSegmentIndex;
    NSLog(@"Index %ld", Index);
    switch (Index) {
        case 0:
            self.scrollView.contentOffset = CGPointMake(0, 0);
            break;
        case 1:
            self.scrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
        default:
            break;
    }
}

#pragma mark - scrollView 的协议方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 当scrollView滚动的时候, 就会调用这个方法
    // 1. 通过偏移量计算当前的页数
    
    NSInteger page = _scrollView.contentOffset.x / self.view.frame.size.width;
    // 2. 给seg赋值
//    NSLog(@"====%ld", page);
    self.seg.selectedSegmentIndex = page;
    
}

- (void)dealloc
{
    [_view1 release];
    [_view2 release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
